package cs.sxh.controller;

import cs.sxh.pojo.User;
import cs.sxh.service.MenuService;
import cs.sxh.service.UserService;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author songxh
 * @create 2022-03-22-20:25
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;


    @RequestMapping("/index")
    public String toLogin(){
        return "user/login";
    }

    //登录处理
    @RequestMapping("/login")
    public String login(String Uid , String Password, Model model, HttpSession session){
        User user = userService.queryUserByUidAndPassword(Uid, Password);
        if (user!=null){
            session.setAttribute("usermsg",user);
            return "redirect:/menu/home";
        }else {
            model.addAttribute("err","用户id或密码错误");
            return "user/login";
        }
    }


    @RequestMapping("/userinfo")
    public String userInfo(Model model,HttpSession session){
        User user1 = (User) session.getAttribute("usermsg");
        int id = user1.getId();
        User user2 = userService.queryUserById(id);
        model.addAttribute("user",user2);
        return "menu/userinfo";
    }


    //修改密码
    @RequestMapping("/password")
    public String updatePassword(String oldpassword,String repassword ,HttpSession session,Model model){
        User user = (User) session.getAttribute("usermsg");
        String uid = user.getUid();
        User user1 = userService.queryUserById(user.getId());

        if (oldpassword.equals(user1.getU_password())){
            userService.updatePassword(uid,repassword);
            return "redirect:/user/userinfo";
        }else {
            model.addAttribute("errpwd","旧密码错误！");
            return "menu/password";
        }
    }

    //用户管理
    @RequestMapping("/users")
    public String Users(Model model){
        List<User> users = userService.queryAllUser();
        model.addAttribute("users",users);
        return "user/users";
    }

    //删除用户
    @RequestMapping("/delete")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "redirect:/user/users";
    }

    @RequestMapping("/toUpdate")
    public String toUpdatePage(int id,Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "user/update";
    }


    @RequestMapping("/updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/user/users";

    }

    @RequestMapping("/toRegister")
    public String toRegisterPage(){
        return "user/register";
    }

    @RequestMapping("/register")
    public String register(User user){
        userService.addUser(user);
        return "user/login";
    }

}
